var data1 = [{'id':'1','operationtype':'修改类型','operationman':'修改人员','operationdate':'time','modificationcontent':'内容','customername':'text1','email':'text2','cucustomerpost':'text3','phone':'text4','mobilephone':'text5','customerwechat':'text6','expressaddress':'text7','customerbirthday':'text8','customerremark':'text9','purchaseman':'text10'}];

var layer = layui.layer //弹层
    ,table = layui.table //表格
    ,upload = layui.upload
    ,userphotos="",
    form = layui.form,$ = layui.jquery,id=getQueryVariable("id");
//自定义表单验证
form.verify({
    validateBankCard: function(value, item){ //value：表单的值、item：表单的DOM对象
        if(!/^([1-9]{1})(\d{14}|\d{18})$/.test(value) && value!= ""){
            return '请输入正确的银行卡账号';
        }
    }
});
//表单初始值，修改时调用
if(id!=""){
    form.val("editor-form",{
        "customerabbreviation":"进入修改客户",
    })
    form.render();
}

//---客户查询
table.render({
    elem: '#supplierselect'
    ,data:data1
    ,toolbar: '#suppliertoolbar'
    ,cols: [[
        {checkbox: true, fixed: true}
        ,{field:'customername', title:'联系人名称', sort: true}
        ,{field:'email', title:'邮箱地址', sort: true}
        ,{field:'cucustomerpost', title:'职务', sort: true}
        ,{field:'phone', title:'电话', sort: true}
        ,{field:'mobilephone', title:'手机', sort: true}
        ,{field:'customerwechat', title:'微信', sort: true}
        ,{field:'expressaddress', title:'快件地址', sort: true}
        ,{field:'customerbirthday', title:'联系人生日', sort: true}
        ,{field:'customerremark', title:'联系人备注', sort: true}
        ,{field:'purchaseman', title:'采购员', sort: true}
        ,{fixed: 'right', width:150, align:'center', toolbar: '#supplierup', title:'操作'}
    ]]
    ,id: 'suppliertableid'
    ,page: true
    ,height:300
});
table.on('toolbar(suppliertable)', function(obj){
    var checkStatus = table.checkStatus(obj.config.id);
    var data = checkStatus.data;
    switch(obj.event){
        case 'new':
            layer.open({
                type: 2,
                area: ['908px', '650px'],
                content: "views/jsp/BasicData/tpl/newsupplier.jsp"
            });
            break;
        case 'delet':
            var data=checkStatus.data,delList=[];
            data.forEach(function(n,i){
                delList.push(n.id);
            });
            if(delList!=''){
                layer.confirm('确定删除所选项吗？',function (index) {
                    var cbList = table.cache["suppliertableid"];
                    for(var i = 0;i<cbList.length;i++){
                        delList.forEach((item, index) => {
                            //当数组中任意一个元素的rowIndex值与取消复选框的元素对象属性rowIndex的下标值相等，则在数组中删除该元素
                            if (typeof (cbList[i]) != "undefined"&&item == cbList[i].id) {
                                cbList.splice(i, 1);
                            }
                        })
                    }
                    table.reload("suppliertableid", {
                        data: cbList,
                    });
                    layer.msg('删除成功');
                })
            }else{
                layer.msg('请选择需要删除的行');
            }
            break;
    };
});
table.on('tool(suppliertable)', function(obj){
    var data = obj.data;
    if(obj.event === 'supplierup') {
        layer.open({
            type: 2,
            area: ['908px', '650px'],
            content: "views/jsp/BasicData/tpl/newsupplier.jsp?id="+obj.data.id
        });
    }else if(obj.event === 'supplierde') {
        layer.confirm('真的删除行么', function (index) {
            obj.del();
            layer.close(index);
        });
    }
});

//---银行信息
table.render({
    elem: '#bankselect'
    ,data:data1
    ,toolbar: '#banktoolbar'
    ,cols: [[
        {checkbox: true, fixed: true}
        ,{field:'bankname', title:'银行名称', sort: true}
        ,{field:'bankaccount', title:'银行帐号', sort: true}
        ,{field:'accountname', title:'账号姓名', sort: true}
        ,{field:'remark', title:'备注', sort: true}
        ,{fixed: 'right', width:150, align:'center', toolbar: '#bankup', title:'操作'}
    ]]
    ,id: 'banktableid'
    ,page: true
    ,height:350
});
table.on('toolbar(banktable)', function(obj){
    var checkStatus = table.checkStatus(obj.config.id);
    var data = checkStatus.data;
    switch(obj.event){
        case 'new':
            layer.open({
                type: 2,
                area: ['908px', '430px'],
                content: 'views/jsp/BasicData/tpl/newbankinfo.jsp'
            });
            break;
        case 'delet':
            var data=checkStatus.data,delList=[];
            data.forEach(function(n,i){
                delList.push(n.id);
            });
            if(delList!=''){
                layer.confirm('确定删除所选项吗？',function (index) {
                    var cbList = table.cache["banktableid"];
                    for(var i = 0;i<cbList.length;i++){
                        delList.forEach((item, index) => {
                            //当数组中任意一个元素的rowIndex值与取消复选框的元素对象属性rowIndex的下标值相等，则在数组中删除该元素
                            if (typeof (cbList[i]) != "undefined"&&item == cbList[i].id) {
                                cbList.splice(i, 1);
                            }
                        })
                    }
                    table.reload("banktableid", {
                        data: cbList,
                    });
                    layer.msg('删除成功');
                })
            }else{
                layer.msg('请选择需要删除的行');
            }
            break;
        case 'searchs':
            var cucustomers = $('#cucustomers').val();
            var cucustomerstype = $('#cucustomerstype option:selected').val();
            var salesman = $('#salesman option:selected').val();
            layer.msg("搜索"+cucustomers+","+cucustomerstype+","+salesman);
            //执行重载
            table.reload('banktableid', {
                data:data1 ,
                page: {
                    curr: 1 //重新从第 1 页开始
                }
                ,where: {
                    'text': cucustomers,
                    'text2': cucustomerstype,
                    'text3' : salesman
                }
            }, 'data');
            break;
    };
});
table.on('tool(banktable)', function(obj){
    var data = obj.data;
    if(obj.event === 'bankup') {
        layer.open({
            type: 2,
            area: ['908px', '420px'],
            content: "views/jsp/BasicData/tpl/newbankinfo.jsp?id="+obj.data.id
        });
    }else if(obj.event === 'bankde') {
        layer.confirm('真的删除行么', function (index) {
            obj.del();
            layer.close(index);
        });
    }
});

//---日志
table.render({
    elem: '#infoselect'
    ,data:data1
    ,cols: [[
        {field:'operationtype', title:'操作类型', sort: true}
        ,{field:'operationman', title:'操作员', sort: true}
        ,{field:'operationdate', title:'操作日期', sort: true}
        ,{field:'modificationcontent', title:'修改内容', sort: true}
    ]]
    ,id: 'contacttableid'
    ,page: true
    ,height:350
});

//上传图片
upload.render({
    elem: '.uploadimg'
    , url: 'https://httpbin.org/post'
    ,before: function(obj) {
        var thin = this;
        obj.preview(function(index, file, result){
            $("#"+thin.id+"").attr('src', result); //图片链接（base64）
        });

    }
    ,done: function(res){
        //如果上传失败
        if(res.code > 0){
            return layer.msg('上传失败');
        }
        //上传成功
    }
    ,error: function(){
        layer.msg('上传失败,请稍后重试')
    }
});

$("#userphotosdel").on('click',function () {
    $("#userphotos").attr('src','');
    userphotos = "";
});
$("#shared").on('click',function () {
    layer.open({
        type: 2,
        area: ['1488px', '800px'],
        content: 'views/jsp/BasicData/tpl/shared.jsp'
    });
});
$("#shutdown").on('click',function () {
    parent.detab("views/jsp/BasicData/tpl/supplierinfo.jsp"+(id==''?'':'?id='+id));
});
$("#print").on("click",function () {
    if(id==""){
        layer.msg('新增不可打印');
    }else{
        layer.open({
            type: 2,
            area: ['1488px', '800px'],
            content: 'views/jsp/BasicData/tpl/print.jsp?id='+id
        });
    }
});

//监听提交----提交审核
form.on('submit(supplierinfo-form-submit)', function(data){
    layer.msg('提交审核')
    var supplier =  layui.table.cache["suppliertableid"]; //联系人表格数据
    setTimeout(function () {
        //保存成功则刷新提交过来的界面 并关闭此页面
        parent.detab("views/jsp/BasicData/customercenter.jsp");
        parent.intab("views/jsp/BasicData/customercenter.jsp","客户中心")
        parent.detab("views/jsp/BasicData/tpl/supplierinfo.jsp"+(id==''?'':'?id='+id));
    },1000)
    return false;
});
//监听提交---保存
form.on('submit(supplierinfo-form-save)', function(data){
    layer.msg('保存')
    var supplier =  layui.table.cache["suppliertableid"]; //联系人表格数据
    setTimeout(function () {
        //保存成功则刷新提交过来的界面 并关闭此页面
        parent.detab("views/jsp/BasicData/customercenter.jsp");
        parent.intab("views/jsp/BasicData/customercenter.jsp","客户中心")
        parent.detab("views/jsp/BasicData/tpl/supplierinfo.jsp"+(id==''?'':'?id='+id));
    },1000)
    return false;
});